// pages/binduser/BindUser.js
var config = require('../../config.js');
var request = require('../../utils/request.js');
var interval = null; //倒计时函数
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    code: '',
    codevalue: '获取验证码',
    currentTime: 61,
    disable: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
 

  /**
   * 获取验证码
   */
  getCodeAction: function () {

    if (!this.data.disable) {
      return;
    }

    if (this.data.phone == ''){
      wx.showToast({
        title: '手机号码不能为空',
        icon:'none'
      })
      return;
    }

    var params = {
      phone: this.data.phone,
      shopId: app.globalData.shopId
    }
    var url = config.smsUrl;
    var self = this;
    request.requestPost(url, params).then(data => {
      wx.showToast({
        title: '验证码发送成功',
      })
      self.setData({
        disable: false
      })
      self.startCode();
    }).catch(msg => {
      console.log(msg);
    })
  },

  startCode: function () {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        codevalue: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          codevalue: '重新发送',
          currentTime: 61,
          disable: true
        })
      }
    }, 1000)
  },

  changenumAction: function (event) {
    const id = event.currentTarget.dataset.id;
    var va = (event.detail.value);
    if (id == 'phone') {
      this.setData({
        phone: va
      })
    } else {
      this.setData({
        code: va
      })
    }
  },

  /**
   * 绑定用户
   */
  bindUserAction:function(){
    var phone = this.data.phone;
    var code = this.data.code;
    var openid = wx.getStorageSync('openId');
    var name = app.globalData.userInfo.nickName;
    var protrait = app.globalData.userInfo.avatarUrl;
    var sex = app.globalData.userInfo.sex;
    var shopkey = wx.getStorageSync("shopkey");

    var params = {
      phone:phone,
      code:code,
      openId:openid,
      name:name,
      protrait: protrait,
      sex:sex,
      shopId: app.globalData.shopId
    }
    if (shopkey != undefined){
      params.shoppingKey = shopkey
    }
    console.log(params);
    request.requestPost(config.bindUserUrl , params)
    .then(data =>{
      console.log('绑定成功');
      console.log(data);

      wx.setStorage({
        key: 'shopkey',
        data: data.shoppingKey,
        success: function () {
          console.log('shopkey 存储成功')
        },
        fail: function () {
          console.log('shopkey 存储失败')
        }
      })
      wx.setStorage({
        key: 'token',
        data: data.token,
        success: function () {
          console.log('token 存储成功')
        },
        fail: function () {
          console.log('token  存储失败');
        }
      })

      wx.navigateBack();
    }).catch(msg => {
      console.log('错误' + msg);    
    })
  }

})